www.gusucode.com > VC++ 汽配销售管理系统(Access)源码程序 > VC++ 汽配销售管理系统(Access)源码程序/源码/RxGrid.cpp

    //Download by http://www.NewXing.com
// RxGrid.cpp : implementation file
//

#include "stdafx.h"
#include "qpglxt.h"
#include "RxGrid.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// RxGrid

RxGrid::RxGrid()
{
	BEditing=false;
}

RxGrid::~RxGrid()
{
}


BEGIN_MESSAGE_MAP(RxGrid, CListCtrl)
	//{{AFX_MSG_MAP(RxGrid)
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// RxGrid message handlers

void RxGrid::PreSubclassWindow() 
{
	this->ModifyStyle(LVS_EDITLABELS,0L); //标题栏不可编辑
	this->ModifyStyle(0L,LVS_REPORT);     
	this->ModifyStyle(0L,LVS_SHOWSELALWAYS);    //高亮显示被选中项
	this->SetExtendedStyle(LVS_EX_FULLROWSELECT| //允许整行选中
		LVS_EX_HEADERDRAGDROP|  //允许整列拖动
		LVS_EX_GRIDLINES|     //画出网格线
		LVS_EX_ONECLICKACTIVATE|     //单击选中项
		LVS_EX_FLATSB);    //扁平风格显示滚动条
	int style=WS_CHILD|WS_CLIPSIBLINGS|WS_EX_TOOLWINDOW|WS_BORDER;
	m_Edit.Create(style,CRect(0,0,0,0),this,ID_CELL);
	CListCtrl::PreSubclassWindow();
}

void RxGrid::OnLButtonDown(UINT nFlags, CPoint point) 
{
	bool bSelected=false;
	if(BEditing==true)     //已经有单元格被编辑
		goto End;
		LVHITTESTINFO testinfo;
		testinfo.pt.x=point.x;
		testinfo.pt.y=point.y;
		testinfo.flags=LVHT_ONITEMLABEL;
		if(this->SubItemHitTest(&testinfo)<0)
			goto End;
		m_Col=testinfo.iSubItem;
		m_Row=testinfo.iItem;
		BEditing=BeginEdit(m_Row,m_Col);     //编辑单元格
		return;
End:	CListCtrl::OnLButtonDown(nFlags, point);
}

bool RxGrid::BeginEdit(int Row,int Col)
{
	if(m_bReadOnly==true)
		return false;
	CRect rect;
	CString sText;
	if(this->GetSubItemRect(Row,Col,LVIR_LABEL,rect)==false)
		return false;
	sText=this->GetItemText(Row,Col);
	m_Edit.MoveWindow(rect);
	m_Edit.SetWindowText(sText);
	m_Edit.ShowWindow(SW_SHOW);
	m_Edit.SetSel(0,-1);
	m_Edit.SetFocus();
	this->GetCols();
	::SendMessage(this->GetParent()->GetSafeHwnd(),DIY_SETFOCUS,NULL,NULL);

	return true;
}

bool RxGrid::EndEdit()
{
	CString sLabel;
	m_Edit.GetWindowText(sLabel);
	this->SetItemText(m_Row,m_Col,sLabel);
	m_Edit.ShowWindow(SW_HIDE);
	BEditing=false;
	::SendMessage(this->GetParent()->GetSafeHwnd(),DIY_KILLFOCUS,NULL,NULL);

	return true;
}

int RxGrid::GetCol()
{
	return m_Col;
}

int RxGrid::GetRow()
{
	return m_Row;
}

int RxGrid::GetRows()
{
	return this->GetItemCount();
}

int RxGrid::GetCols()
{
	int cols[255];
	this->GetColumnOrderArray(&cols[0]);
	for(int m=0;m<255;m++)
	{
		if(cols[m]==-858993460)
			break;
	}	
	return m;
}

bool RxGrid::SetRow(int nRow)
{
	this->EndEdit();
	this->m_Row=nRow;	
	this->BeginEdit(m_Row,m_Col);
	return true;
}

bool RxGrid::SetCol(int nCol)
{
	this->EndEdit();
	this->m_Col=nCol;	
	this->BeginEdit(m_Row,m_Col);
	return true;
}

BOOL RxGrid::PreTranslateMessage(MSG* pMsg) 
{
	return CListCtrl::PreTranslateMessage(pMsg);
}

void RxGrid::MoveNextItem()
{
	CSize size;
	int ncol;	
	ncol=GetCol();
	if(ncol<GetCols()-1)
	{
		size.cx=20*ncol;
		this->Scroll(size);
		SetCol(ncol+1);	
	}
	else
	{
		size.cx=-20*m_Col;
		this->Scroll(size);
		if(GetRow()<GetRows()-1)
		{	
			m_Col=0;
			SetRow(GetRow()+1);	
		}
		else
		{
			this->InsertItem(GetRow()+1,"");
			this->SetCol(0);
			this->SetRow(m_Row+1);
		}
	}
}

void RxGrid::SetDataBase(CString Record,long adCmd)
{
	rst.Open(Record,adCmd);
	CString sFieldName;
	for(int m=0;m<rst.GetFieldCount();m++)
	{
		sFieldName=rst.GetFieldName(m);
		this->InsertColumn(m,sFieldName);
	}
	this->AddCellValue(rst);

}

void RxGrid::ReadOnly(bool bReadOnly)
{
	m_bReadOnly=bReadOnly;
}

void RxGrid::AddCellValue(RxRecordset rs)
{
	CString sFieldName;
	int i,m;
	rs.MoveFirst();
	this->DeleteAllItems();
	for(i=0;i<rs.GetRecordCount();i++)
	{
		this->InsertItem(i,"");
	}
	for(i=0;i<rs.GetRecordCount();i++)
	{
		rs.Move(i);
		for(m=0;m<rs.GetFieldCount();m++)
		{
			sFieldName=rs.GetFieldName(m);		
			this->SetItemText(i,m,rs.GetFieldValue(sFieldName));
		}
	}
	for(m=0;m<rs.GetFieldCount();m++)
	{
		this->SetColumnWidth(m,LVSCW_AUTOSIZE_USEHEADER );
	}
}